﻿using System;
using System.Collections.Generic;
using ShkSimulation.core.factory;
using ShkSimulation.core.template.agency;
using ShkSimulation.core.template.customer;
using ShkSimulation.core.util;

namespace ShkSimulation.carsharing.impl.create {
	
	public class DefaultDemandManager : DemandManagerByCreateParam {
		
		public static readonly string DemandId = "Id";
		public static readonly string DemandOriginId = "OriginId";
		public static readonly string DemandDestinationId = "DestinationId";
		public static readonly string DemandValueKey = "DemandValue";
		public static readonly string DemandUnit = "Unit";
		public static readonly string DemandInitWhenStart = "InitWhenStart";
		public static readonly string DemandRoadIds = "RoadIds";
		
		public DefaultDemandManager(string demandResourceKey) : base(demandResourceKey) {
		}

		public override DemandManager Clone() {
			return new DefaultDemandManager(this.demandResourceKey);
		}

		public override CustomerTemplate CreateTemplate(CreateParam createParam) {
			Dictionary<string, object> lineData = createParam.Params;
			int id = Convert.ToInt32(lineData[DemandId]);
			int origin = Convert.ToInt32(lineData[DemandOriginId]);
			int destination = Convert.ToInt32(lineData[DemandDestinationId]);
			double demand = Convert.ToDouble(lineData[DemandValueKey]);
			string timeUnit = Convert.ToString(lineData[DemandUnit]);
			TimeSpan demandTimeSpan = DemandToTimeSpanWithTimeUnit(demand, timeUnit);
			bool initWhenStart = Convert.ToBoolean(lineData[DemandInitWhenStart]);
			List<int> internalAgencyIds = CollectionUtil.StringToList(Convert.ToString(lineData[DemandRoadIds]));

			List<Agency> agencies = new List<Agency>();
			agencies.Add(NetworkInputData.AllAgencies[origin]);
			foreach (var agencyId in internalAgencyIds) {
				agencies.Add(NetworkInputData.AllAgencies[agencyId]);
			}
			agencies.Add(NetworkInputData.AllAgencies[destination]);
			TravelerTemplate template = new TravelerTemplate(id, agencies);
			// TODO 和道路的价格等有关系
			template.Demand = demand;
			template.DemandTimeSpan = demandTimeSpan;
			template.InitWhenStart = initWhenStart;
			return template;
		}

	}
}